package com.frx.sort.myqueue;

import java.util.Arrays;

/**
 * Created with IDEA
 *
 * @author: xrf
 * @Date: 2019/10/25
 * @Time: 14:24
 */
public class ArrayQueue {
    private int capacity;
    int[] arr;
    private int head = 0;
    private int tail = 0;

    public ArrayQueue(int capacity){
        arr = new int[capacity];
        this.capacity = capacity;
    }
    public static void main(String[] args){
        ArrayQueue arrayQueue = new ArrayQueue(4);
        arrayQueue.enqueue(1);
        arrayQueue.enqueue(2);
        arrayQueue.enqueue(3);
        arrayQueue.enqueue(4);
        System.out.println(Arrays.toString(arrayQueue.arr));
        System.out.println(arrayQueue.dequeue());
        System.out.println(arrayQueue.dequeue());
        arrayQueue.enqueue(5);
        System.out.println(Arrays.toString(arrayQueue.arr));

    }


    public boolean enqueue(int a){
        if (tail == capacity){
            if (head == 0){
                return false;
            } else {
                int N = tail - head;
                for (int i=0;i<N;i++){
                    arr[i] = arr[head + i];
                }
                head = 0;
                tail = N;
            }
        }
        arr[tail++] = a;
        return true;
    }
    public int dequeue(){
        if (head == tail){
            return 0;
        }
        int res = arr[head];
        head ++;
        return res;
    }

}
